﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using MessageBox=System.Windows.Forms.MessageBox;

namespace ScreenGrab
{
  /// <summary>
  /// Interaction logic for options.xaml
  /// </summary>
  public partial class options : Window
  {
    public options()
    {
      InitializeComponent();
      try
      {
        ConfigurationManager.RefreshSection("ImageStore");
        ConfigurationManager.RefreshSection("SaveAs");
        ConfigurationManager.RefreshSection("ImageBurst");
        ConfigurationManager.RefreshSection("ImageBurstFrames");


        ImageDir.Text = ReadSetting("ImageStore");

        txtMultiBurstFrames.Text = ReadSetting("ImageBurstFrames");

        string multiBurstEnabled = ReadSetting("ImageBurst");
        if (multiBurstEnabled == "True")
        {
          cbMultiBurst.IsChecked = true;
        }
        else
        {
          cbMultiBurst.IsChecked = false;
        }


        string saveAs = ReadSetting("SaveAs");
        if (saveAs == "PNG")
          rbPNG.IsChecked = true;
        else
          rbJPG.IsChecked = true;

      }
      catch (Exception)
      {
          
        //throw;
      }


    }

    public static string ReadSetting(string key)
    {
      try
      {
        ImageStoreData configData = ConfigurationManager.GetSection(key) as ImageStoreData;

        return configData.ImageStore;

      }
      catch (Exception)
      {
          
        throw;
      }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      try
      {
        WriteXML(ImageDir.Text,"ImageStore");
        if(cbMultiBurst.IsChecked== true)
        {
          WriteXML("True","ImageBurst");
        }
        else
        {
          WriteXML("False","ImageBurst");
        }
        if (txtMultiBurstFrames.Text != "")
        {
          //MultiBurstFrames
          WriteXML(txtMultiBurstFrames.Text, "ImageBurstFrames");
        }
        else
          WriteXML("0", "ImageBurstFrames");
        if (rbPNG.IsChecked == true)
        {
          WriteXML("PNG","SaveAs");
        }
        else
        {
          WriteXML("JPG", "SaveAs");
        }
        
        //WriteSetting("ImageStore", ImageDir.Text);
      }
      catch(Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
      wndOptions.Close();
    }

    public static void WriteXML(string key,string section)
    {
      ImageStoreData configData = new ImageStoreData();

      configData.ImageStore = key;
      // Write the new configuration data to the XML file
      Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
      config.Sections.Remove(section);
      config.Sections.Add(section, configData);
      config.Save();
    }



    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
      wndOptions.Close();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
      FolderBrowserDialog dlg = new FolderBrowserDialog();
      if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
      {
        string foldername = dlg.SelectedPath;
        ImageDir.Text = foldername;
      }
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
      ConfigurationManager.RefreshSection("ImageStore");
      MessageBox.Show(ReadSetting("ImageStore"));
    }

    private void cbMultiBurst_Checked(object sender, RoutedEventArgs e)
    {
      if ((bool)cbMultiBurst.IsChecked)
        txtMultiBurstFrames.IsEnabled = true;
    }

    private void cbMultiBurst_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
    {

    }

    private void cbMultiBurst_Unchecked(object sender, RoutedEventArgs e)
    {
      if (!(bool)cbMultiBurst.IsChecked)
        txtMultiBurstFrames.IsEnabled = false;
      
    }


  }
}
